package com.hxl.qqclient.view;

import com.hxl.qqclient.service.FileClientService;
import com.hxl.qqclient.service.ManagerClientConnectServerThread;
import com.hxl.qqclient.service.MessageClientService;
import com.hxl.qqclient.service.UserClientService;
import com.hxl.qqclient.utils.Utility;


import java.io.IOException;

/**
 * 客户端的菜单界面
 */
@SuppressWarnings({"all"})
public class QQView {
    private boolean loop = true;//控制是否显示菜单
    private String key = "";
    private UserClientService userClientService = new UserClientService();//对象用于登陆服务/注册用户
    private MessageClientService messageClientService = new MessageClientService();//对象用户私聊/群聊
    private FileClientService fileClientService = new FileClientService();//该对象用于传输文件

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        new QQView().mainMenu();
        System.out.println("客户端退出系统");
    }

    //显示主菜单
    private void mainMenu() throws IOException, ClassNotFoundException {
        while (loop) {
            System.out.println("=========欢迎登陆网络通讯系统========");
            System.out.println("\t\t 1 登陆系统");
            System.out.println("\t\t 9 退出系统");
            System.out.print("请输入你的选择");
            key = Utility.readString(1);
            //根据用户的输入，来处理不同的逻辑
            switch (key) {
                case "1":
                    System.out.print("请输入用户号");
                    String userId = Utility.readString(50);
                    System.out.print("请输入密码");
                    String passwd = Utility.readString(50);
                    //需要到服务端去验证该用户是否合法
                    //编写一个UserClientService【用户登陆/注册】
                    if (userClientService.checkUser(userId, passwd)) {
                        System.out.println("=========欢迎" + userId + "登陆成功========");
                        //进入到二级菜单
                        while (loop) {
                            System.out.println("\n========网路通信系统二级菜单（用户" + userId + ")==========");
                            System.out.println("\t\t 1 显示在线用户列表");
                            System.out.println("\t\t 2 群发消息");
                            System.out.println("\t\t 3 私聊消息");
                            System.out.println("\t\t 4 发送文件");
                            System.out.println("\t\t 9 退出系统");
                            System.out.println("请输入你的选择：");
                            key = Utility.readString(1);
                            switch (key) {
                                case "1":
                                    //获取在线用户列表
                                    userClientService.onlineFriendList();
                                    break;
                                case "2":
                                    System.out.println("请输入想对大家说的话：");
                                    String s = Utility.readString(100);
                                    //调用一个方法，将消息封装成message对象，发给服务端
                                    messageClientService.sendMessageToAll(s, userId);
                                    break;
                                case "3":
                                    System.out.print("请输入想聊天的用户号（在线）：");
                                    String getterId = Utility.readString(50);
                                    System.out.println("请输入想说的话：");
                                    String content = Utility.readString(100);
                                    messageClientService.sendMessageToOne(content, userId, getterId);
                                    //编写一个方法，将消息发送给服务端
                                    break;
                                case "4":
                                    System.out.print("请输入你想把文件发送给的用户（在线用户）");
                                    getterId = Utility.readString(50);
                                    System.out.print("请输入发送文件的路径(形式如d:\\xx.jpg)");
                                    String src = Utility.readString(100);
                                    System.out.print("请输入把文件发送到对方的路径(形式如d:\\xx.jpg)");
                                    String dest = Utility.readString(100);
                                    fileClientService.sendFileToOne(src, dest, userId, getterId);
                                    break;
                                case "9":
                                    System.out.println("退出系统");
                                    //调用方法，给服务器发送一个退出系统的message
                                    userClientService.logout();
                                    loop = false;
                                    break;

                            }
                        }
                    } else {//登陆服务器失败
                        System.out.println("=====登陆失败====");
                    }
                    break;
                case "9":
                    loop = false;
                    break;
            }
        }
    }

}
